package com.yudean.esb.rest.impl;

import com.yudean.esb.service.handler.FWSDataHandler;
import com.yudean.esb.rest.PutService;
import com.yudean.esb.service.ErpClientService;
import com.yudean.esb.service.handler.ReceiveDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * @author DL<duanlei # gdyd.com>
 */
@Controller("webPutServiceImpl")
@Scope("prototype")
public class PutServiceImpl implements PutService {
    private final static Logger log = LoggerFactory.getLogger(PutServiceImpl.class);
    @Resource
    private ReceiveDataService receiveDataServiceImpl;

    // 法务系统数据数据工具
    @Resource
    private FWSDataHandler fwsDataHandler;

    @Resource
    ErpClientService erpClientService;

    @Override
    public String put(String serviceID, String token, String transactionID,
                      String pkg, String totalPackage, String rollback,
                      String forceReplace, String data) throws Exception {
        log.info("request-put():{serviceID:{},token{},transactionID:{},pkg:{},totalPackage:{},rollback:{},forceReplace:{},data:{}}",
                serviceID,token,transactionID,pkg,totalPackage,rollback,forceReplace,data);
        try {
            erpClientService.addErpClientLog(token);
        } catch (Exception e) {
            log.error("save ERP log of /rest/put error:", e);
        }

        // TODO: 这种判断非常耗时，待改进
        // 因为法务系统不愿意做调整的原因，只能为其做单独的
        if ("erp_ids_payments".equals(serviceID)) {

            return fwsDataHandler.handlePaymentsData(data, token);
        } else if ("erp_ids_control".equals(serviceID)) {

            return fwsDataHandler.handleControlData(data, token);
        } else if ("erp_ids_contract".equals(serviceID)) {

            return fwsDataHandler.handleContractData(data, token);
        } else {

            return receiveDataServiceImpl.put(serviceID, token, transactionID, pkg, totalPackage, rollback, forceReplace, data);
        }
    }

    @Override
    public String upload() {
        return "FAILED";
    }
}